package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class WineTypeController {

    // REST operations
    // GET
    def show = {
        println 'WineTypeController >> Show >> START'
        
        WineTypeManagement wtman = new WineTypeManagement()
        
        if (params.id && WineType.exists(params.id)) {    
            def wt = wtman.getWineTypeByID(params.id)
            render wt as JSON
            
            println 'Show wine type by ID >> ' + wt.id + " " + wt
        }
        else {
            def allWT = wtman.listWineTypes()
            if (allWT.length() > 0) {
                render allWT as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            }  
            
            println 'Listing all wine cats >> ' + allWT
        }
        
        println 'WineTypeController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineTypeController >> Update >> START'
        
        JSONObject o
        def wt
        def id
        def wineType
        def wineDesc
        
        WineTypeManagement wtman = new WineTypeManagement()
        
        
        if (params.wineType && params.wineDesc) {
            println 'WineTypeController >> Update >> NO JSON'
            wt = wtman.getWineTypeByID(params.id)      
            wineType = params.wineType
            wineDesc = params.wineDesc
            
//            wt.id = id
            wt.typeName = wineType
            wt.description = wineDesc
        }
        else {
            println 'WineTypeController >> Update >> JSON'
            o = request.JSON
            
            wt = wtman.getWineTypeByID(o.get("id"))
            if (o.has("typeName")) { wt.typeName = o.get("typeName"); println wt.typeName }
            if (o.has("description")) { wt.description = o.get("description"); println wt.description }
        }
        
        def result = wtman.updateWineType(wt)
        if (result == true) {
            println 'WineTypeController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'WineTypeController >> Update >> ' + result
        }
                       
        

        println 'WineTypeController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineTypeController >> delete >> START'
        
        WineTypeManagement wtman = new WineTypeManagement()
        
        if (params.id && WineType.exists(params.id)) {    
            def result = wtman.deleteWineType(id)
            
            if (result == true) {
            println 'WineTypeController >> delete >> ' + result
    //            render response as JSON
            }
            else {
                println 'WineTypeController >> delete >> ' + result
            }
        }
        
        println 'WineTypeController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineTypeController >> Save >> START'
        
//        JSONObject o = request.JSON
        
        WineTypeManagement wtman = new WineTypeManagement()
        def wineType = params['wineType']
        println wineType
        def result = wtman.addWineType(o)
        if (result == true) {
            println 'WineTypeController >> Save >> ' + result
//            render response as JSON
        }
        else {
            println 'WineTypeController >> Save >> ' + result
        }
        
        println 'WineTypeController >> Save >> END'
        redirect(uri:"/wineTypeCreate")
    }
    
    def wineTypeCreate = {
        
    }
}